var io = require('socket.io-client');
var modalController = require('./modal.controller');
merchantController.$inject = ['$scope', '$location', 'merchantService', '$uibModal', '$stateParams','$state'];
function merchantController($scope, $location, merchantService, $modal, $stateParams,$state) {
   
    var vm = this;
    vm.merchant = [];
    vm.merchantStatus = [];

    //home page data
    $scope.merchantsCount;
    $scope.statusOne;
    $scope.statusTwo;
    $scope.statusThree;
    $scope.statusFour;
    $scope.manager = JSON.parse(sessionStorage.getItem("manager"));

    //socket
    //proxy； io.connect("http://10.222.29.156:8888/",'/o2o');
    var socket = io.connect("ws://10.222.29.190:8888/");
    $scope.newRegister = 0;
    socket.on('register', function (msg) {
        $scope.$apply(function(){
            $scope.newRegister++;
            console.log('new register count:' + $scope.newRegister);
        })
    });
    socket.on('disconnect',function(){
        console.log('disconnect');
    });

    //find all merchant list
    vm.findAllMerchantList = function () {
        merchantService.findAllMerchantList(function (error, result) {
            if (error) {
                console.log("error" + error.message);
            }
            else {
                console.log("success to call back");
                vm.merchant = result;
                $scope.newRegister=0;
            }
        });
    };

    //find merchants by status
    vm.findMerchantListByStatus = function (status) {
        merchantService.findMerchantListByStatus(status, function (error, result) {
            if (error) {
                console.log("error" + error.message);
            }
            else {
                console.log("success to call back");
                vm.status = status;
                vm.merchant = result;
            }
        });
    };

    //find merchants by id
    vm.findMerchantListById = function (id) {
        merchantService.findMerchantListById(id, function (error, result) {
            if (error) {
                console.log("error" + error.message);
            }
            else {
                console.log("success to call back");
            }
        });
    };

    //modal
    vm.openMerchantModal = function (merchant) {
        console.log("success to call back");
        var modalInstance = $modal.open({
            template: require('../layouts/merchant.modal.html'),
            controller: modalController,
            resolve: {
                merchant: function () {
                    return merchant;
                },
                changeStatus: function () {
                    return vm.findMerchantListByStatus;
                }
            }
        });
        modalInstance.opened.then(function () {//when the modal open
            console.log('modal is opened');
        });
        modalInstance.result.then(function (result) {
            console.log(result);
        }, function (reason) {
            console.log(reason);
        });
    };

    //home page data init
    function countMerchants() {
        for (let index = 0; index < 4; index++) {
            merchantService.findMerchantListByStatus(index, function (error, result) {
                if (error) {
                    console.log("error" + error.message);
                }
                else {
                    if (index == 0) {
                        $scope.statusOne = result.length;
                    } else if (index == 1) {
                        $scope.statusTwo = result.length;
                    } else if (index == 2) {
                        $scope.statusThree = result.length;
                    } else if (index == 3) {
                        $scope.statusFour = result.length;
                    }
                }
            });
        }
        merchantService.findAllMerchantList(function (error, result) {
            if (error) {
                console.log("error" + error.message);
            }
            else {
                $scope.merchantsCount = result.length;
            }
        });
    }

    //init
    vm.initPage = function () {
        console.log($stateParams.status);
        if ($stateParams.status || $stateParams.status == 0) {
            vm.findMerchantListByStatus($stateParams.status);
        }
        else {
            vm.findAllMerchantList();
        }
    }

    //mangager exit
    vm.exit = function(){
        var flag = confirm('是否退出管理系统？');
        if(flag){
            sessionStorage.removeItem("manager");
            $state.go('login');       
        }
        return 
    }

    //init
    countMerchants();
    vm.initPage();
}
module.exports = merchantController;

